<template>
  <!-- 设备导出对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" label-width="80px">
      <!-- 根据所属产品搜索 -->
      <el-form-item label="产品" prop="productId">
        <el-select v-model="form.productId" placeholder="请选择产品" clearable class="select-width">
          <el-option
            v-for="product in productMap"
            :key="product[0]"
            :label="product[1].productName"
            :value="product[0]"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'DeviceExport',
  data() {
    return {
      title: '导出设备',
      open: false,
      form: {},
      productMap: new Map(),
    }
  },
  watch: {
    open(visible) {
      if (!visible) {
        this.reset()
      }
    }
  },
  methods: {
    submitForm() {
      this.download('iot/device/export', {
        ...this.form
      }, `device_${new Date().getTime()}.xlsx`)
    },
    cancel() {
      this.open = false
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    handleExport(products) {
      this.productMap = products
      this.open = true
    }
  }
}
</script>
